#include "Timer.h"

/**
  * @brief        定时中断3初始化
  * @param Period 定时中断3的间隔时间ms
  * @return       void
  */
void Timer3_Init(int Period)
{
    /*开启时钟*/
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);    // 改用TIM3
    
    /*时基单元初始化*/
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitStructure.TIM_Period = (Period - 1);
    TIM_TimeBaseInitStructure.TIM_Prescaler = 7200 - 1;     // 根据需求调整分频
    TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);     // 初始化TIM3
    
    /*中断输出配置*/
    TIM_ClearFlag(TIM3, TIM_FLAG_Update);                   // 清除TIM3标志位
    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);              // 开启TIM3更新中断
    
    /*NVIC配置*/
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;         // 改为TIM3中断通道
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_Init(&NVIC_InitStructure);
    
    /*TIM使能*/
    TIM_Cmd(TIM3, ENABLE);                                  // 使能TIM3
}

/**
  * @brief        定时中断4初始化
  * @param Period 定时中断4的间隔时间ms
  * @return       void
  */
void Timer4_Init(int Period)
{
    /*开启时钟*/
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);    // 改用TIM4
    
    /*时基单元初始化*/
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitStructure.TIM_Period = 10 * (Period - 1);
    TIM_TimeBaseInitStructure.TIM_Prescaler = 7200 - 1;     // 根据需求调整分频
    TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure);     // 初始化TIM4
    
    /*中断输出配置*/
    TIM_ClearFlag(TIM4, TIM_FLAG_Update);                   // 清除TIM4标志位
    TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);              // 开启TIM4更新中断
    
    /*NVIC配置*/
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;         // 改为TIM4中断通道
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_Init(&NVIC_InitStructure);
    
    /*TIM使能*/
    TIM_Cmd(TIM4, ENABLE);                                  // 使能TIM4
}

/* 定时器中断3函数，可以复制到使用它的地方
void TIM3_IRQHandler(void)
{
	if (TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)
	{
		
		TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
	}
}
*/

/* 定时器中断4函数，可以复制到使用它的地方
void TIM4_IRQHandler(void)
{
	if (TIM_GetITStatus(TIM4, TIM_IT_Update) == SET)
	{
		
		TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
	}
}
*/
